package risk.control.application.component.load;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import risk.control.application.R;
import risk.control.application.component.loadview.CycleLoadView;


/**
 * CreationTime:2025/4/23
 * Describe:
 * Author:hm
 **/

public class LoadDialog extends Dialog implements DialogInterface.OnDismissListener {

    private static final int width = 170;
    private static final int height = 170;
    private static final int style = R.style.Dialog;
    private static final boolean isCancelable = false;

    private final View mRootView;
    private CycleLoadView mCycleLoadView;

    public LoadDialog(Context context) {
        super(context, style);
        mRootView = View.inflate(context, R.layout.load_dialog, null);
        setContentView(mRootView);
        setCancelable(isCancelable);
        setCanceledOnTouchOutside(isCancelable);
        Window window = getWindow();
        if (window != null) {
            // 设置对话框的位置：居中
            WindowManager.LayoutParams params = window.getAttributes();
            float density = getDensity(context);
            params.width = (int) (width * density);
            params.height = (int) (height * density);
            params.gravity = Gravity.CENTER;
            window.setAttributes(params);
            initView();
            initData();
        }
    }

    private void initView() {
        mCycleLoadView = mRootView.findViewById(R.id.CycleLoadView);
    }

    private void initData() {
        mCycleLoadView.showLoad();
    }

    private static float getDensity(Context context) {
        Resources resources = context.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        return dm.density;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        mCycleLoadView.stopLoad();
    }

    public void showDialog() {
        mCycleLoadView.showLoad();
        this.show();
    }

    public void showDialog(String des) {
        mCycleLoadView.setDes(des);
        mCycleLoadView.showLoad();
        this.show();
    }
}
